package autoFile.core.strategy;

import autoFile.core.strategy.interfaces.CreateFileStrategyInterface;
import com.google.common.base.CaseFormat;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: liuliwei
 * Date: 2017-08-16
 * Time: 下午4:29
 * Description: ${description}
 */
public class CreateDbConstantsStrategy extends AbstractStrategy implements CreateFileStrategyInterface {

    String fileName = "/Users/liuliwei/autoFile/GongDbConstants.java";

    private FileWriter dbConstantWriter = new FileWriter(fileName, true);

    public CreateDbConstantsStrategy() throws IOException {
    }

    @Override
    public void createFile(Class clazz) throws IOException {
        List<Method> publicMethod = getPublicMethod(clazz);
        String className = clazz.getSimpleName(); //类名称
        System.out.println("生成 "+fileName);
        for (Method method : publicMethod) {
            if (className.contains("Service")) {
                className = className.replaceFirst("Service", "");
                className = className.replaceFirst("Db", "Command");
            }
            System.out.print(createDbConstants(className, method, dbConstantWriter));

        }
        System.out.println();
        System.out.println();
        dbConstantWriter.close();
    }

    private static String createDbConstants(String simpleName, Method method, FileWriter fileWriter) throws IOException {
        simpleName = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL).convert(simpleName);
        String methodName = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_CAMEL).convert(method.getName());
        String name = simpleName + methodName;
        String returnString = "GongDbConstants." + CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_UNDERSCORE).convert(name);
        String pre = "public static final String ";
        String dbConstant = pre + CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_UNDERSCORE).convert(name) + "= \"ar." + name + "\";\n";
        fileWriter.write(dbConstant);
        return dbConstant;
    }
}
